//
//  ShellLoginViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellLoginViewController.h"
#import "ShellAccountTextField.h"
#import "ShellForgetViewController.h"
#import "ShellRegisteredViewController.h"
#import "ShellLanguageViewController.h"
#import "ShellAccountViewModel.h"

@interface ShellLoginViewController ()

@property (nonatomic,strong) ShellAccountViewModel *viewModel;

@end

@implementation ShellLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = LZLocalizedString(@"登录");

    [self setupView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = COLOR_BACKGROUND;
}

- (void)setupView{
    
    ///!!!:切换语言
    UIButton *languageBtn = [UIButton lz_buttonTitle:kShellLanguageManager.language titleColor:UIColorHex(0x8D99B2) fontSize:14];
    languageBtn.frame = CGRectMake(0, 0, 100, 20);
    languageBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [languageBtn setImage:k_imageName(@"icon_language") forState:UIControlStateNormal];
    [languageBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:9];
    [self initBarItem:languageBtn withType:1];
    
    
    
    ///取消
    UIButton *cancelBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"取消") titleColor:UIColorHex(0xFFFFFF) fontSize:14];
    cancelBtn.frame = CGRectMake(0, 0, 100, 20);
    cancelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self initBarItem:cancelBtn withType:0];
    
    
    ///!!!:logo
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = k_imageName(@"icon_logo");
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(54);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(100);

    }];
    
    
    ///!!!:邮箱
    ShellAccountTextField *accountTextField = [[ShellAccountTextField alloc] init];
    accountTextField.placeholder = LZLocalizedString(@"邮箱");
    accountTextField.keyboardType = UIKeyboardTypeEmailAddress;
    [self.view addSubview:accountTextField];
    [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(50);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    
    ///!!!:密码
    ShellAccountTextField *passwordTextField = [[ShellAccountTextField alloc] init];
    passwordTextField.secureTextEntry = YES;
    [self.view addSubview:passwordTextField];
    passwordTextField.keyboardType = UIKeyboardTypeASCIICapable;
    passwordTextField.placeholder = LZLocalizedString(@"密码");
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(accountTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    ///!!!:登录
    UIButton *loginBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"登录") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    [loginBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self.view addSubview:loginBtn];
    loginBtn.cornerRadius = 6;
    loginBtn.layer.shadowOffset = CGSizeMake(0,2);
    loginBtn.layer.shadowOpacity = 1;
    loginBtn.layer.shadowRadius = 6;
    loginBtn.enabled = NO;
    [loginBtn setTitleColor:RGBA(255, 255, 255, 0.5) forState:UIControlStateDisabled];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordTextField.mas_bottom).mas_offset(26);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    
    
    ///!!!:去注册
    UIButton *registeredBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"没有账号?马上注册") titleColor:UIColorHex(0x8D99B2) fontSize:17];
    registeredBtn.backgroundColor = UIColorHex(0x0D3354);
    [self.view addSubview:registeredBtn];
    registeredBtn.cornerRadius = 6;
    [registeredBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(loginBtn.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    ///!!!:忘记密码
    UIButton *forgetBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"忘记密码?") titleColor:UIColorHex(0x4AB2E7) fontSize:14];
    [self.view addSubview:forgetBtn];
    forgetBtn.cornerRadius = 6;
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(registeredBtn.mas_bottom).mas_offset(23);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(179);
        make.height.mas_equalTo(25);
    }];
    
    
    //登录按钮的控制
    [[accountTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(accountTextField.text.length > 6 && passwordTextField.text.length >= 6){
            loginBtn.enabled = YES;
        } else {
            loginBtn.enabled = NO;
        }
    }];
    
    [[passwordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(accountTextField.text.length > 6 && passwordTextField.text.length >= 6){
            loginBtn.enabled = YES;
        } else {
            loginBtn.enabled = NO;
        }
    }];
    
    
    @weakify(self);
    [[loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        ///!!!:去登录
        [self.view endEditing:YES];
        [self.viewModel loginAccount:accountTextField.text password:passwordTextField.text];
    }];
    
    [[registeredBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        ///!!!:去注册
        @strongify(self);
        ShellRegisteredViewController *registeredVC = [[ShellRegisteredViewController alloc] init];
        [self.navigationController pushViewController:registeredVC animated:YES];
    }];
    
    [[forgetBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        ///!!!:忘记密码
        @strongify(self);
        ShellForgetViewController *forgetVC = [[ShellForgetViewController alloc] init];
        [self.navigationController pushViewController:forgetVC animated:YES];
        
    }];
    
    
    
    ///!!!:选择语言
    [[languageBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        ShellLanguageViewController *languageVC = [[ShellLanguageViewController alloc] init];
        [self.navigationController pushViewController:languageVC animated:YES];

    }];
    
    
    ///!!!:取消
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    ///!!!:国际化
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        self.title = LZLocalizedString(@"登录");
        
        [cancelBtn setTitle:LZLocalizedString(@"取消") forState:UIControlStateNormal];
        [languageBtn setTitle:kShellLanguageManager.language forState:UIControlStateNormal];
        [languageBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:9];
        accountTextField.placeholder = LZLocalizedString(@"邮箱");
        passwordTextField.placeholder = LZLocalizedString(@"密码");
        [loginBtn setTitle:LZLocalizedString(@"登录") forState:UIControlStateNormal];
        [registeredBtn setTitle:LZLocalizedString(@"没有账号?马上注册") forState:UIControlStateNormal];
        [forgetBtn setTitle:LZLocalizedString(@"忘记密码?") forState:UIControlStateNormal];
    }];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


//viewModel;
- (ShellAccountViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellAccountViewModel alloc] init];
    }
    return _viewModel;
}

/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
